package c.c.a;

import android.os.Bundle;
import android.text.TextUtils;
import com.bytedance.embedapplog.AppLog;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

public class y1 {
    public static long n;
    public static b o;
    public final g2 a;
    public final c b;
    public a0 c;
    public a0 d;
    public String e;
    public long f;
    public int g;
    public long h = -1;
    public volatile boolean i;
    public long j;
    public int k;
    public String l;
    public y m;

    public static class b extends d0 {
        public b(a aVar) {
        }
    }

    public y1(c cVar, g2 g2Var) {
        this.b = cVar;
        this.a = g2Var;
    }

    public static long a(g2 g2Var) {
        long j = n + 1;
        n = j;
        if (j % 1000 == 0) {
            Objects.requireNonNull(g2Var);
        }
        return n;
    }

    public static boolean d(s sVar) {
        return sVar instanceof a0 ? ((a0) sVar).m() : false;
    }

    public synchronized Bundle b(long j, long j2) {
        Bundle bundle;
        if (this.a.b.isPlayEnable()) {
            Object obj = (this.i && this.j == 0) ? 1 : null;
            if (obj != null && j - this.f > j2) {
                bundle = new Bundle();
                bundle.putInt("session_no", this.k);
                int i = this.g + 1;
                this.g = i;
                bundle.putInt("send_times", i);
                bundle.putLong("current_duration", (j - this.f) / 1000);
                bundle.putString("session_start_time", s.h.format(new Date(this.h)));
                this.f = j;
            }
        }
        bundle = null;
        return bundle;
    }

    public final synchronized void c(s sVar, ArrayList<s> arrayList, boolean z) {
        StringBuilder stringBuilder;
        long j = sVar instanceof b ? -1 : sVar.a;
        this.e = UUID.randomUUID().toString();
        Objects.requireNonNull(this.a);
        n = 10000;
        this.h = j;
        this.i = z;
        this.j = 0;
        if (m0.b) {
            stringBuilder = new StringBuilder();
            stringBuilder.append("startSession, ");
            stringBuilder.append(this.e);
            stringBuilder.append(", hadUi:");
            stringBuilder.append(z);
            stringBuilder.append(" data:");
            stringBuilder.append(sVar);
            m0.a(stringBuilder.toString(), null);
        }
        if (z) {
            Calendar instance = Calendar.getInstance();
            stringBuilder = new StringBuilder();
            stringBuilder.append("");
            stringBuilder.append(instance.get(1));
            stringBuilder.append(instance.get(2));
            stringBuilder.append(instance.get(5));
            String stringBuilder2 = stringBuilder.toString();
            if (TextUtils.isEmpty(this.l)) {
                this.l = this.a.d.getString("session_last_day", "");
                this.k = this.a.d.getInt("session_order", 0);
            }
            if (stringBuilder2.equals(this.l)) {
                this.k++;
            } else {
                this.l = stringBuilder2;
                this.k = 1;
            }
            this.a.d.edit().putString("session_last_day", stringBuilder2).putInt("session_order", this.k).apply();
            this.g = 0;
        }
        if (j != -1) {
            y yVar = new y();
            yVar.c = this.e;
            yVar.b = a(this.a);
            yVar.a = this.h;
            yVar.j = this.b.f();
            yVar.i = this.b.e();
            if (this.a.e()) {
                yVar.e = AppLog.getAbConfigVersion();
                yVar.f = AppLog.getAbSDKVersion();
            }
            arrayList.add(yVar);
            this.m = yVar;
            if (m0.b) {
                StringBuilder stringBuilder3 = new StringBuilder();
                stringBuilder3.append("gen launch, ");
                stringBuilder3.append(yVar.c);
                stringBuilder3.append(", hadUi:");
                stringBuilder3.append(z);
                m0.a(stringBuilder3.toString(), null);
            }
        }
    }
}
